'''
Created on 02/11/2012

@author: GabrielBacelar
'''

class HW4Q2(object):
    '''
    classdocs
    '''


    def __init__(self):
        '''
        Constructor
        '''
        
    def getHW4Q2(self):
        
        import PolicyIteration
        self.policyIteration = PolicyIteration.PolicyIteration()
        
        self.policyIteration.rewardruns = 0
        self.policyIteration.policyruns = 0
        self.policyIteration.actionruns = 0
        
        self.policyIteration.actions = ["CW","CCW"]
        self.policyIteration.nodes = ["A","B","C"]
        
        self.policyIteration.gama = 0.5
        
        """
        s     a      s'     T(s,a,s')     R(s,a,s')
        A     CW     B     1.0     2.0
        A     CCW     B     0.2     0.0
        A     CCW     C     0.8     1.0
        B     CW     A     0.2     2.0
        B     CW     C     0.8     0.0
        B     CCW     A     1.0     1.0
        C     CW     A     0.6     -2.0
        C     CW     B     0.4     1.0
        C     CCW     A     0.4     0.0
        C     CCW     B     0.6     -1.0
        """
        
        self.policyIteration.T,self.policyIteration.R = {},{}
        self.policyIteration.T[("A","CW","B")] = 1.0
        self.policyIteration.R[("A","CW","B")] = 2.0
        self.policyIteration.T[("A","CCW","B")] = 0.2
        self.policyIteration.R[("A","CCW","B")] = 0.0
        self.policyIteration.T[("A","CCW","C")] = 0.8
        self.policyIteration.R[("A","CCW","C")] = 1.0
        self.policyIteration.T[("B","CW","A")] = 0.2
        self.policyIteration.R[("B","CW","A")] = 2.0
        self.policyIteration.T[("B","CW","C")] = 0.8
        self.policyIteration.R[("B","CW","C")] = 0.0
        self.policyIteration.T[("B","CCW","A")] = 1.0
        self.policyIteration.R[("B","CCW","A")] = 1.0
        self.policyIteration.T[("C","CW","A")] = 0.6
        self.policyIteration.R[("C","CW","A")] = -2.0
        self.policyIteration.T[("C","CW","B")] = 0.4
        self.policyIteration.R[("C","CW","B")] = 1.0
        self.policyIteration.T[("C","CCW","A")] = 0.4
        self.policyIteration.R[("C","CCW","A")] = 0.0
        self.policyIteration.T[("C","CCW","B")] = 0.6
        self.policyIteration.R[("C","CCW","B")] = -1.0
        
        self.policyIteration.V = {}
        self.policyIteration.Vstar = {}
        
        self.policyIteration.V["A"] = 2.2
        self.policyIteration.V["B"] = 0.36
        self.policyIteration.V["C"] = -0.08
        
        self.policyIteration.Vstar["A"] = 2.329
        self.policyIteration.Vstar["B"] = 0.658
        self.policyIteration.Vstar["C"] = 0.063
        
        self.policyIteration.policies = {}
        self.policyIteration.policies["A"] = "CW"
        self.policyIteration.policies["B"] = "CW"
        self.policyIteration.policies["C"] = "CCW"
        
        
    def resolveHW4Q2(self):
        self.getHW4Q2()
        
        print " "
        print "Policy Iteration"
        pit = self.policyIteration
        node = "A"
        print " "
        print node, pit.getPolicyIteration(node)
        print "Policy Itarations: ", pit.policyruns, "Action Itarations:", pit.actionruns, "Reward Itarations:", pit.rewardruns
        
        self.getHW4Q2()
        pit = self.policyIteration
        node = "C"
        print " "
        print node, pit.getPolicyIteration(node)
        print "Policy Itarations: ", pit.policyruns, "Action Itarations:", pit.actionruns, "Reward Itarations:", pit.rewardruns
                
        print " "
        print "Value Iteration"
        self.getHW4Q2()
        pit = self.policyIteration
        node = "A"
        print " "
        print node, pit.getValueIteration(node)
        print "Policy Itarations: ", pit.policyruns, "Action Itarations:", pit.actionruns, "Reward Itarations:", pit.rewardruns
        
        self.getHW4Q2()
        pit = self.policyIteration
        node = "C"
        print " "
        print node, pit.getValueIteration(node)
        print "Policy Itarations: ", pit.policyruns, "Action Itarations:", pit.actionruns, "Reward Itarations:", pit.rewardruns
        
hw4q2 = HW4Q2()
hw4q2.resolveHW4Q2()
        